<template>
  <el-dialog
    title="新建目录"
    :visible.sync="dialogVisible"
    width="500px"
    center:true
    :before-close="handleClose"
  >
    <el-form label-position="left" label-width="100px" :rules="rules" :model="info" ref="infoForm">
      <el-row>
        <el-col :span="20">
          <el-form-item label="父目录:">
            <label style="float:left;">{{pDir.name}}</label>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item label="名称:" prop="name">
            <el-input placeholder="请输入名称" v-model="info.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">关闭</el-button>
      <el-button type="primary" @click="handleSave()">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      info: {},
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
        ]
      }
    };
  },
  //父组件传过来的数据属性
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    pDir: {
      type: Object,
      required: true
    }
  },
   watch: {
    pDir(val) {
      this.info = {};
    }
  },
  components: {},
  //方法
  methods: {
    //关闭按钮
    handleClose() {
      this.$emit("dialogClose");
    },
    //保存按钮
    handleSave() {
      this.$refs.infoForm.validate(valid => {
        if (!valid) {
          return false;
        }
        let _this = this;
        this.axios
          .post(this.HOST + "/dir/create", {
            pid: this.pDir.id,
            name: this.info.name
          })
          .then(function(res) {
            if (!res.data.status) {
              _this.$alert(res.data.msg, "提示", {
                confirmButtonText: "确定"
              });
              return;
            }
            _this.$message({
              showClose: true,
              message: "保存成功",
              type: "success"
            });
            _this.$emit("dialogSave");
          })
          .catch(function(err) {
            _this.$message.error("操作失败" + err);
          });
      });
    }
  }
};
</script>
<style >
</style>
